package com.study_platform.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study_platform.entity.ProblemSolvingRecord;
import com.study_platform.entity.StudyRecord;
import com.study_platform.service.ProblemSolvingRecordService;
import com.study_platform.service.QuestionService;
import com.study_platform.service.StudyRecordService;
import com.study_platform.util.VO.RecordInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/study_platform/study-record")
public class StudyRecordController {
    @Resource
    private StudyRecordService studyRecordService;


    @PostMapping("/record/")//打卡记录
    public JSONObject record(@RequestBody RecordInfo recordInfo){
        StudyRecord record = null;
        Integer time = recordInfo.getTime();
        Integer user_id = recordInfo.getUserId();
        List<StudyRecord> list = studyRecordService.lambdaQuery()
                .eq(StudyRecord::getUserId, user_id)
                .orderByDesc(StudyRecord::getDate).list();
        if (list != null && list.size() > 0){
            record = list.get(0);
        }else{
            record = new StudyRecord(null, user_id, new Date(), time);
        }
        Date today = new Date();

        if (record.getDate().getYear() == today.getYear()
                && record.getDate().getMonth() == today.getMonth()
                && record.getDate().getDay() == today.getDay()){
            record.setStudyTime(record.getStudyTime() + time);
        }else{
            record = new StudyRecord(null, user_id, new Date(), time);
        }
        studyRecordService.saveOrUpdate(record);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("record", record);
        jsonObject.put("error_message", "success");
        return jsonObject;
    }

    @GetMapping("/getDays/{user_id}/")
    public JSONObject getDays(@PathVariable Integer user_id){
        Integer count = studyRecordService.lambdaQuery()
                .eq(StudyRecord::getUserId, user_id)
                .count();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        return jsonObject;
    }

    @GetMapping("/getContinuousDays/{user_id}/")
    public JSONObject getContinuousDays(@PathVariable Integer user_id){
        List<StudyRecord> records = studyRecordService.lambdaQuery()
                .eq(StudyRecord::getUserId, user_id)
                .orderByDesc(StudyRecord::getDate)
                .list();
        long today = new Date().getTime();
        Integer count = 0;
        JSONObject jsonObject = new JSONObject();
        for (StudyRecord record : records){
            if (today - record.getDate().getTime() <= 24 * 60 * 60 * 1000){
                count ++;
                today -= 24 * 60 * 60 * 1000;
            }else{
                break;
            }
        }
        jsonObject.put("count", count);
        return jsonObject;
    }
}

